﻿using System;
using System.Web.UI;

namespace MikeKappel.Com.CS
{
    public static partial class Javascript
    {
        public static void RegisterControl(Control cntrl)
        {
            ScriptManager CurrentScriptManager = ScriptManager.GetCurrent(cntrl.Page);
            string script = "";
            if ((CurrentScriptManager != null && ((ScriptManager)CurrentScriptManager).IsInAsyncPostBack))
            {
                if (cntrl is System.Web.UI.WebControls.BaseValidator)
                {
                    script = "if (MIKE.exists('" + cntrl.ID + "')){" + cntrl.ID + " = document.getElementById('" + cntrl.ClientID + "');}else{var " + cntrl.ID + " = document.getElementById('" + cntrl.ClientID + "');}";
                }
                else
                {
                    script = "if (MIKE.exists('" + cntrl.ID + "')){" + cntrl.ID + " = ObjectVar('" + cntrl.ClientID + "');}else{var " + cntrl.ID + " = ObjectVar('" + cntrl.ClientID + "');}";
                }
                ScriptManager.RegisterClientScriptBlock(cntrl.Page, cntrl.Page.GetType(), "var" + cntrl.ID, script + "\n", true);
            }
            else
            {
                if (cntrl is System.Web.UI.WebControls.BaseValidator)
                {
                    script = "var " + cntrl.ID + " = " + cntrl.ClientID + ";";
                }
                else
                {
                    script = "var " + cntrl.ID + " = document.getElementById('" + cntrl.ClientID + "');";
                }
                cntrl.Page.ClientScript.RegisterStartupScript(cntrl.Page.GetType(), "var" + cntrl.ID, script + "\n", true);
            }
        }
	}
}
